// pages/mine/history/index.js
import { request } from '../../../api/request.js'
import { formatDate } from '../../../uits/formatDate.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    queryList: {
      billTime: '',
      currentPage: 1,
      pageSize: 10,
      payStatus: -1
    },
    historyBillList: [],
    popupShow: false,
    sheetShow: false,
    option1: [
      { name: '全部', value: -1 },
      { name: '已结算', value: 1 },
      { name: '待结算', value: 0 },
    ],
    selectName: '',
    url: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    let date = formatDate(new Date())
    let billTime = "queryList.billTime"
    this.setData({
      [billTime]: date.slice(0, 7),
    });

    this.onPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    request({
      url: "/bill/historyBill",
      data: this.data.queryList
    }).then(res => {
      console.log(res.data)
      this.setData({
        historyBillList: res.data
      })
      wx.hideLoading()
      wx.stopPullDownRefresh()
    }).catch(err => {
      wx.hideLoading()
    })
  },
  // 订单详情
  dayOrder(e) {
    wx.showLoading({
      title: '加载中',
      success: (res => {
        if (res.errMsg === "showLoading:ok") {
          wx.navigateTo({
            url: "../zhangdanxiangqin/index?orderId=" + e.currentTarget.dataset.index.orderId,
            success: (res => {
              if (res.errMsg === "navigateTo:ok") {
                wx.hideLoading()
              }
            })
          })
        }
      })
    })

  },
  // 订单时间
  onConfirm(e) {
    let date = formatDate(e.detail)
    let billTime = "queryList.billTime"
    this.setData({
      [billTime]: date.slice(0, 7),
    });
    this.setData({ popupShow: false });
    this.onReady()
  },
  // 时间框取消
  onCancel() {
    this.setData({ popupShow: false });
  },
  // 打开时间弹出框
  dateOrder() {
    this.setData({ popupShow: true });
  },
  // 结算状态
  onSelect(e) {
    this.setData({ selectName: e.detail.name })
    let payStatus = "queryList.payStatus"
    this.setData({
      [payStatus]: e.detail.value,
    });

    request({
      url: "/bill/historyBill",
      data: this.data.queryList
    }).then(res => {
      console.log(res.data)
      this.setData({
        historyBillList: res.data
      })
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
    })

    this.setData({ sheetShow: true });
  },
  onClose() {
    this.setData({ sheetShow: false });
  },
  // 导出
  excel() {
    request({
      url: "/user/personalCenter",
      data: {
        billTime: this.data.queryList.billTime
      }
    }).then(res => {
      console.log(res.data.picUrl)
      this.setData({
        url: res.data.picUrl
      })
      // wx.hideLoading()
    }).catch(err => {
      // wx.hideLoading()
    })
    wx.downloadFile({
      // 示例 url，并非真实存在
      url: this.data.url, //仅为示例，并非真实的资源
      success: function (res) {
        console.log('下载成功', res)
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功', res)
          },
          fail: function (err) {
            console.log('fail', err)
          },
          complete: function (r) {
            console.log('complete', r)
          }
        })
      },
      fail() {
        console.log('文件下载失败');
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ historyBillList: [] })
    this.onReady()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})